﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Navigation;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using YJTTool.DBBaseServer.AbstractModel;
using YJTTool.MainWindow.Model;
using AutoMapper;
using NetTaste;
using YJTTool.DrugControls.Views;
using YJTTool.CustomerInfo.Views;

namespace YJTTool.MainWindow.ViewModels
{
    public class NavigatePageViewModel : BindableBase, INavigationAware
    {
        #region 内部字段
        private readonly IRegionManager _regionManager;
        private readonly AbstractUserConfigServer abstractUserConfig;
        private  bool isClick = false;
        #endregion

        private UserConfigDto userConfig;

        public UserConfigDto UserConfig
        {
            get => userConfig;
            set 
            {
                userConfig = value;
                RaisePropertyChanged();
                if (userConfig != null)
                {
                    UpdataPage(userConfig.ModuleName);
                }
            }
        }
        public ObservableCollection<UserConfigDto> PagesCollectionView { get; private set; } = new ObservableCollection<UserConfigDto>();

        public NavigatePageViewModel(AbstractUserConfigServer abstractUserConfig, IMapper mapper, IRegionManager regionManager)
        {
            this.abstractUserConfig = abstractUserConfig;

            _regionManager = regionManager;

            InitConfig();
        }
        private void InitConfig() 
        {
            var data = abstractUserConfig.Query();
            foreach (var item in data)
            {
                PagesCollectionView.Add(new UserConfigDto()
                {
                    Id = item.Id,
                    ModuleName = item .ModuleName,
                    ModuleCode = item .ModuleCode,
                    Date = item .Date,
                    UserInfo = item .UserInfo
                });
            }
            if (PagesCollectionView.Count > 0)
            {
                UserConfig = PagesCollectionView.FirstOrDefault(s => s.ModuleName == "客户信息");
            }
        }

        private void UpdataPage(string page)
        {
            try
            {
                switch (page)
                {
                    case "药品对照":
                        _regionManager.RequestNavigate("ContentRegion", "DrugControlsView");
                        break;
                    case "客户信息":
                        _regionManager.RequestNavigate("ContentRegion", "CustomerInfoView");
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("导航失败: " + ex.Message);
            }
        }
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {

        }
    }
}
